import dt from '@dt/dt'
import ruleOpt from '@/biz/option/rule.js'

let _this = null

function agency(opt) {
  console.log(opt, opt.key1)
  let gap = {
    placeholder: '至',
    col: 2,
    visible: opt.key1 == '1',
    text: {
      textAlign: 'center',
    }
  }
  let start = {
    key: opt.key2,
    col: 6,
    input: {
      type: 'number',
    },
  }
  let end = {
    key: opt.key3,
    col: 6,
    visible: opt.key1 == '1',
    input: {
      type: 'number',
    }
  }
  let unit = {
    placeholder: _this.info[opt.key4] == '1' ? opt.unit : opt.secondUnit,
    col: 3,
    text: {}
  }
  let option = {
    key: opt.key4,
    title: opt.title,
    group: [{
      key: opt.key4,
      col: 7,
      select: {
        options: [{ key: 1, value: '比例' }, { key: 2, value: '固定金额' }],
      },
      change: (res) => {
        _this.info[opt.key2] = null
        _this.info[opt.key3] = null
        unit.placeholder = _this.info[opt.key4] == 1 ? opt.unit : opt.secondUnit
      }
    }, start, gap, end, unit],
    rules: [
      {
        required: true,
        message: '请填写代理费',
        trigger: 'blur'
      },
      ruleOpt.check('请填写完整的区间值', () => {
        if (opt.key1 == '1') {
          return (_this.info[opt.key3] == null && _this.info[opt.key2] != null) ||
            (_this.info[opt.key3] != null && _this.info[opt.key2] == null)
        }
      }),
      ruleOpt.check('不允许输入负数', () => {
        if (opt.key1 == '1') {
          return _this.info[opt.key3] < 0 || _this.info[opt.key2] < 0
        }
      }),
      ruleOpt.check('起始值须小于结束值', () => {
        if (opt.key1 == '1') {
          return _this.info[opt.key3] < _this.info[opt.key2]
        }
      }),
    ]
  }
  console.log(option, '****')
  return option
}

function p1() {
  let opt = [{
    title: '合作模式',
    placeholder: '请选择合作模式',
    key: 'coopMode',
    col: 12,
    select: {
      options: dt.dictInfo().cooperationModeAttrSort,
    },
    rules: [{
      required: true,
      message: '请选择合作模式',
      trigger: 'blur'
    }]
  }, {
    title: '销售模式',
    key: 'sellMode',
    col: 12,
    select: {
      options: dt.dictInfo().salesModel,
    },
    rules: [{
      required: true,
      message: '请选择销售模式',
      trigger: 'blur'
    }]
  }]
  return opt
}

function p2() {
  let productType = {
    title: '产品类型',
    placeholder: '请选择产品类型',
    key: 'productType',
    col: 12,
    disabled: _this.isdisable,
    select: {
      options: dt.dictInfo().productType,
    },
    load: (o) => {
      if (_this.info.productType) {
        buildingTypes.select.options = _this.productTypeChange()
      }
    },
    change: (data) => {
      buildingTypes.select.options = _this.productTypeChange()
    },
    rules: [{
      required: true,
      message: '请选择产品类型',
      trigger: 'blur'
    }]
  }
  let buildingTypes = {
    title: '建筑类型',
    key: 'buildingTypes',
    placeholder: '请选择产品类型',
    col: 12,
    disabled: _this.isdisable,
    select: {
      options: [],
      multiple: true,
    },
    rules: [{
      required: true,
      type: 'array',
      message: '请选择建筑类型',
      trigger: 'blur'
    }]
  }
  let opt = [productType, buildingTypes]
  return opt
}

function p3() {
  let opt = [{
    title: '装修情况',
    placeholder: '请选择装修情况',
    key: 'decorateSituation',
    col: 12,
    select: {
      options: dt.dictInfo().productRenovation,
    },
    rules: [{
      required: true,
      message: '请选择装修情况',
      trigger: 'blur'
    }]
  }, {
    title: '产权年限',
    key: 'property',
    col: 12,
    input: {
      appendTxt: '年',
    },
    rules: [{
        required: true,
        message: '请输入产权年限',
        trigger: 'blur'
      },
      ruleOpt.check('请输入不超过三位的数字', (val) => {
        let reg = /^[0-9]{0,3}$/
        return !reg.test(val)
      })
    ]
  }]
  return opt
}

function p4(_this) {
  let opt = [{
    title: '是否有跳帕佣金',
    key: 'isJumpCommission',
    col: 12,
    radio: {
      options: [{ key: '1', value: '是' }, { key: '0', value: '否' }],
    },
    change: (val) => {
      agency({
        title: '代理费',
        key1: _this.info.isJumpCommission,
        key2: 'commissionFeesStartValue',
        key3: 'commissionFeesEndValue',
        key4: 'commissionFeesType',
        unit: '%',
        secondUnit: '元'
      })
      _this.info.commissionFeesStartValue = null
      _this.info.commissionFeesEndValue = null
      _this.info.commissionFeesType = '1'
      _this.update()
    },
    rules: [{
      required: true,
      message: '请选择是否有跳帕佣金',
      trigger: 'blur'
    }]
  }, agency({
    title: '代理费',
    key1: _this.info.isJumpCommission,
    key2: 'commissionFeesStartValue',
    key3: 'commissionFeesEndValue',
    key4: 'commissionFeesType',
    unit: '%',
    secondUnit: '元'
  })]
  console.log(opt, '&&&&')
  return opt
}

function p5() {
  let opt = [{
    title: '总货量(套数)',
    key: 'totalCargoQuantityCount',
    col: 12,
    input: {
      appendTxt: '套数',
      maxlength: 20
    },
    rules: [
      ruleOpt.check('数值只能为正整数', (value) => {
        if (!value) return false;
        let reg = /^[1-9]\d*$/
        return !reg.test(value)
      })
    ]
  }, {
    title: '总货量(面积)',
    key: 'totalCargoQuantityArea',
    col: 12,
    input: {
      appendTxt: '㎡',
      maxlength: 20
    },
    rules: [
      ruleOpt.check('数值只能保留两位小数', (value) => {
        if (!value) return false;
        let reg = /^[\d,-]?\d+(\.\d{1,2}){0,1}$/
        return !reg.test(value)
      })
    ]
  }]
  return opt
}

function p6() {
  let opt = [{
    title: '总货值(价值)',
    key: 'totalCargoQuantityValue',
    col: 12,
    input: {
      appendTxt: '元',
      maxlength: 20
    },
    rules: [
      ruleOpt.check('数值只能保留两位小数', (value) => {
        if (!value) return false;
        let reg = /^[\d,-]?\d+(\.\d{1,2}){0,1}$/
        return !reg.test(value)
      })
    ]
  }, {
    title: '单边货量(套数)',
    key: 'unitCargoQuantityCount',
    col: 12,
    input: {
      appendTxt: '套数',
      maxlength: 20
    },
    rules: [
      ruleOpt.check('数值只能为正整数111', (value) => {
        if (!value) return false;
        let reg = /^[1-9]\d*$/
        return !reg.test(value)
      })
    ]
  }]
  return opt
}

function p7() {
  let opt = [{
    title: '单边货量(面积)',
    key: 'unitCargoQuantityArea',
    col: 12,
    input: {
      appendTxt: '㎡',
      maxlength: 20
    },
    rules: [
      ruleOpt.check('数值只能保留两位小数', (value) => {
        if (!value) return false;
        let reg = /^[\d,-]?\d+(\.\d{1,2}){0,1}$/
        return !reg.test(value)
      })
    ]
  }, {
    title: '单边货值(价值)',
    key: 'unitCargoQuantityValue',
    col: 12,
    input: {
      appendTxt: '元',
      maxlength: 20
    },
    rules: [
      ruleOpt.check('数值只能保留两位小数', (value) => {
        if (!value) return false;
        let reg = /^[\d,-]?\d+(\.\d{1,2}){0,1}$/
        return !reg.test(value)
      })
    ]
  }]
  return opt
}

function p8() {
  let opt = [{
    title: '物业公司',
    key: 'propertyCompany',
    col: 12,
    input: {},
    rules: [
    {
      max: 20,
      message: '最多20个字',
      trigger: 'blur,change'
    }]
  }, {
    title: '物业费',
    key: 'propertyFree',
    col: 12,
    input: {
      appendTxt: '元/㎡',
    },
    rules: [
      ruleOpt.check('数值只能保留两位小数', (value) => {
        if (!value) return false;
        let reg = /^[\d,-]?\d+(\.\d{1,2}){0,1}$/
        return !reg.test(value)
      })
    ]
  }]
  return opt
}

function p9() {
  let opt = [{
    title: '月末指标计算',
    key: 'isIndexCal',
    col: 12,
    radio: {
      options: [{ key: '1', value: '参与' }, { key: '2', value: '不参与' }],
    }
  }]
  return opt
}

function options(vm) {
  _this = vm
  let arr = [{
      title: '产品名称',
      placeholder: '请输入产品名称',
      key: 'productName',
      disabled: _this.isdisable,
      input: { maxlength: 20 },
      rules: [{
        required: true,
        message: '请输入产品名称',
        trigger: 'blur'
      }]
    },
    p1(),
    p2(),
    p3(),
    p4(_this),
    {
      title: '联代公司简称',
      placeholder: '请输入联代公司简称',
      key: 'generationCompany',
      input: {
        maxlength: 20
      }
    },
    p5(),
    p6(),
    p7(),
    p8(),
    p9()
  ]
  return arr
}

export default {
  options
}